package com.bestcem.xm.award.service.impl;

import cn.hutool.core.date.DateUtil;
import com.bestcem.xm.award.dao.AwardAwardDao;
import com.bestcem.xm.award.dao.AwardRequestDao;
import com.bestcem.xm.award.dao.AwardRequestStatsDayDao;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.entity.pojo.AwardRequestStatsDayDO;
import com.bestcem.xm.award.service.AwardStatsService;
import com.bestcem.xm.award.service.dto.awardaward.GetRequestStatsDTO;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/10/14 15:54
 */
@Service
@Slf4j
public class AwardStatsServiceImpl implements AwardStatsService {

    private static final Logger EMAIL_LOG = LoggerFactory.getLogger("EmailErrorLog");

    @Autowired
    private AwardRequestStatsDayDao awardRequestStatsDayDao;

    @Autowired
    private AwardAwardDao awardAwardDao;

    @Autowired
    private AwardRequestDao awardRequestDao;

    private static final Integer BATCH_SIZE = 100;


    @Override
    public ServiceResult dailyRequestStats(Date day) {
        // 时分秒置0
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        day = calendar.getTime();
        try {
            awardRequestStatsDayDao.deleteByStatsDay(day);
            int page = 1;
            int size = 100;
            boolean flag = true;
            while (flag) {
                List<AwardAwardDO> awardDOS = awardAwardDao.listAwardsByStartAndEndDay(null, day, page++, size);
                if (CollectionUtils.isEmpty(awardDOS)) {
                    log.info("[award] 奖励每日统计， 日期:{}, 分页:{},该日没有更多要统计的奖励数据", DateUtil.formatDate(day), page);
                    return ServiceResult.success();
                }
                List<AwardRequestStatsDayDO> batchList = new ArrayList<>();
                for (AwardAwardDO awardDO : awardDOS) {
                    GetRequestStatsDTO requestCountDay = awardRequestDao.getRequestCountDay(awardDO.getId(), null, day);
                    AwardRequestStatsDayDO awardRequestStatsDay = new AwardRequestStatsDayDO();
                    awardRequestStatsDay.setOrgId(awardDO == null ? null : awardDO.getOrgId());
                    awardRequestStatsDay.setProjectId(awardDO == null ? null : awardDO.getProjectId());
                    awardRequestStatsDay.setAwardId(awardDO.getId());
                    awardRequestStatsDay.setTotal(requestCountDay == null || requestCountDay.getTotalCount() == null ? 0 : requestCountDay.getTotalCount());
                    awardRequestStatsDay.setPass(requestCountDay == null || requestCountDay.getPassCount() == null ? 0 : requestCountDay.getPassCount());
                    awardRequestStatsDay.setStatsDay(day);
                    batchList.add(awardRequestStatsDay);
                }
                awardRequestStatsDayDao.batchInsert(batchList);
                if (awardDOS.size() < size) {
                    flag = false;
                }
            }

            return ServiceResult.success();
        } catch (Exception e) {
            EMAIL_LOG.error("[award] 每日统计奖励请求数据" + DateUtil.formatDate(day), e);
            log.error("[award] 每日统计奖励请求数据" + DateUtil.formatDate(day), e);
            return ServiceResult.fail("奖励每日请求异常失败");
        }

    }
}
